package com.yygo.service.impl;

import com.yygo.dao.LoanProductDao;
import com.yygo.model.BondCompany;
import com.yygo.model.LoanProduct;
import com.yygo.model.vo.LoanProductVO;
import com.yygo.service.BondManager;
import com.yygo.service.LoanProductManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by tiansha on 2015/6/26.
 */
@Service("loanProductManager")
public class LoanProductManagerImpl extends GenericManagerImpl<LoanProduct, Long> implements LoanProductManager {

    private LoanProductDao loanProductDao;

    @Autowired
    private BondManager bondManager;

    @Autowired
    public LoanProductManagerImpl(LoanProductDao loanProductDao) {
        super(loanProductDao);
        this.loanProductDao = loanProductDao;
    }

    @Override
    public LoanProduct loadLoanProduct(Long id) {
        return loanProductDao.loadLoanProduct(id);
    }

    @Override
    @Cacheable(value = "loanProductsByBank", key = "#bank")
    public List<LoanProduct> getLoanProductsByBond(Long bondId, boolean isSecond) {
        Set<LoanProduct> loanProducts = this.loanProductDao.getLoanProductsByBond(bondId, isSecond);
        List<LoanProduct> loanProductList = new ArrayList<LoanProduct>();
      for (LoanProduct loanProduct: loanProducts){
        if(loanProduct.isEnabled()){
          loanProductList.add(loanProduct);
        }
      }
        //loanProductList.addAll(loanProducts);
        loanProductList.sort(new Comparator<LoanProduct>() {
            @Override
            public int compare(LoanProduct o1, LoanProduct o2) {
                return o1.getProductName().compareTo(o2.getProductName());
            }
        });
        return loanProductList;
    }

    @Override
    @Cacheable(value = "loanProduct", key = "#id")
    public LoanProductVO getById (Long id){
        LoanProduct loanProduct =  this.loanProductDao.get(id);
      LoanProductVO loanProductVO = new LoanProductVO();
      loanProductVO.setId(loanProduct.getId());
      loanProductVO.setCostRate(loanProduct.getCostRate());
      loanProductVO.setSecond(loanProduct.isSecond());
      loanProductVO.setCoef(loanProduct.getCoef());
      loanProductVO.setCrLL(loanProduct.getCrLL());
      loanProductVO.setCrUL(loanProduct.getCrUL());
      loanProductVO.setDpLL(loanProduct.getDpLL());
      loanProductVO.setDpUL(loanProduct.getDpUL());
      loanProductVO.setFaLL(loanProduct.getFaLL());
      loanProductVO.setFaUL(loanProduct.getFaUL());
      loanProductVO.setLoanYears(loanProduct.getLoanYears());
      loanProductVO.setProductName(loanProduct.getProductName());
      return loanProductVO;
    }

    @Override
    public List<LoanProduct> getAll(boolean deleted) {
        return loanProductDao.getAll(deleted);
    }

    @Override
    public void saveLoanProduct(LoanProduct loanProduct) {
        /*Set<BondCompany> oldBonds = new HashSet<>();
        if (loanProduct.getId() != null) {
            LoanProduct oldProduct = loadLoanProduct(loanProduct.getId());
            oldBonds.addAll(oldProduct.getBonds());
        }

        for (BondCompany bondCompany : oldBonds) {
            BondCompany bond = bondManager.loadBond(bondCompany.getId());
            bond.getLoanProducts().remove(loanProduct);
            bondManager.save(bond);
        }*/

        loanProduct.getBonds().clear();
        if (loanProduct.getBondIds() != null ){
            Set<BondCompany> list = new HashSet<>();
            for (Long bondId : loanProduct.getBondIds()){
                BondCompany bond = bondManager.load(bondId);
                //bond.getLoanProducts().add(loanProduct);
                //bondManager.save(bond);
                list.add(bond);
            }
            loanProduct.setBonds(list);
        }
        save(loanProduct);
    }

}
